package top.carrotguo.weibo.presenter;

import android.text.TextUtils;

import com.sina.weibo.sdk.auth.Oauth2AccessToken;

import top.carrotguo.weibo.bean.CommentResultBean;
import top.carrotguo.weibo.contract.SendCommentContract;
import top.carrotguo.weibo.model.SendCommentModel;
import top.carrotguo.weibo.retrofit.callback.BaseCallback;
import top.carrotguo.weibo.retrofit.callback.ErrorInfo;

public class SendCommentPresenter extends BasePresenter<SendCommentContract.View>
        implements SendCommentContract.Presenter {

    private SendCommentContract.Model mModel;

    public SendCommentPresenter () {
        mModel = new SendCommentModel();
    }

    /**
     * 发表评论
     * @param token
     * @param blogId
     */
    @Override
    public void sendComment(Oauth2AccessToken token, String blogId) {
        if (getView() != null) {
            //显示正在发送中
            getView().showSending();
            //评论内容不能是空
            if (TextUtils.isEmpty(getView().getTextContent())) {
                getView().showNoSendEmpty();
                return;
            }
            mModel.sendComment(token, blogId, getView().getTextContent(), new BaseCallback<CommentResultBean, ErrorInfo>() {
                @Override
                public void onSuccessed(CommentResultBean successInfo) {
                    if (getView() != null) {
                        //隐藏发送中
                        getView().hideSending();
                        if (TextUtils.isEmpty(successInfo.getError_code())) {
                            getView().showSendSuccess();
                        } else {
                            getView().showSendError(new ErrorInfo(Integer.parseInt(successInfo.getError_code()),successInfo.getError()));
                        }
                    }
                }

                @Override
                public void onError(ErrorInfo errorInfo) {
                    if (getView() != null) {
                        //隐藏发送中
                        getView().hideSending();
                        getView().showSendError(errorInfo);
                    }
                }
            });
        }
    }
}
